package weaver.jobs.afilm.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.afilm.duss.utils.MaycurUtil;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-02
 * @Description: OA批量同步部门到每刻接口，OA员工批量同步到每刻接口
 * @ModifyLog:
 **/

public class WeaSyncToMaycurCronJob extends BaseCronJob {
    @Override
    public void execute() {
        String fullDate = DateUtil.getFullDate();
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>fullDate=" + fullDate);
        JSONObject responseJson = MaycurUtil.getToken();
        baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>getToken>>>>responseJson=" + responseJson);
        String status = Util.null2String(responseJson.getString("success"));
        if ("true".equals(status)) {
            JSONObject dataJson = responseJson.getJSONObject("data");
            String entCode = Util.null2String(dataJson.getString("entCode"));
            String tokenId = Util.null2String(dataJson.getString("tokenId"));
            String tlevels = MaycurUtil.getTlevel();
            String[] tlevelList = tlevels.split(",");
            baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>departmentBatchAdd>>>>tlevels=" + tlevels + "<br/>");
            JSONArray departmentsList = new JSONArray();
            for (String tlevel : tlevelList) {
                baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>departmentBatchAdd>>>>tlevel=" + tlevel + "<br/>");
                //同步部门到每刻
                JSONArray departmentList = new JSONArray();
                rs.executeQuery("select a.id,a.departmentcode,a.departmentname,a.subcompanyid1,a.canceled,a.supdepid " +
                        "from hrmdepartment a join hrmdepartmentdefined b on a.id=b.deptid " +
                        "where isnull(b.sfytb,1) !=0 and a.tlevel=?", tlevel);
                while (rs.next()) {
                    JSONObject departmentJson = new JSONObject();
                    String departid = Util.null2String(rs.getString("id"));
                    String departmentcode = Util.null2String(rs.getString("departmentcode"));
                    String departmentname = Util.null2String(rs.getString("departmentname"));
                    String subcompanyid1 = Util.null2String(rs.getString("subcompanyid1"));
                    String canceled = Util.null2String(rs.getString("canceled"));
                    String supdepid = Util.null2String(rs.getString("supdepid"));
                    departmentJson.put("departid", departid);
                    departmentJson.put("businessCode", departmentcode);
                    departmentJson.put("newBusinessCode", "");
                    departmentJson.put("name", departmentname);
                    String parentBizCode = "100" + subcompanyid1;
                    if (!"0".equals(supdepid)) {
                        parentBizCode = MaycurUtil.getDepartmentCode(supdepid);
                    }
                    departmentJson.put("parentBizCode", parentBizCode);
                    departmentJson.put("enabled", !"0".equals(canceled));
                    departmentList.add(departmentJson);
                    departmentsList.add(departmentJson);
                }
                baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>departmentBatchAdd>>>>departmentList=" + departmentList + "<br/>");
                if (departmentList.size() > 0) {
                    String responseStr = MaycurUtil.departmentBatch(departmentList, entCode, tokenId);
                    baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>departmentBatchAdd>>>>responseStr=" + responseStr + "<br/>");
                    JSONObject depresponseJson = JSONObject.parseObject(responseStr);
                    String depstatus = depresponseJson.getString("success");
                    String depdata = depresponseJson.getString("data");
                    if ("true".equals(depstatus)) {
                        rs.executeUpdate("update hrmdepartmentdefined set sfytb=0 where isnull(sfytb,1) !=0 and deptid in (select id from hrmdepartment where tlevel='" + tlevel + "')");
                    }
                    if ("false".equals(depstatus)) {
                        rs.executeUpdate("update hrmdepartmentdefined set sfytb=0 where isnull(sfytb,1) !=0 and deptid in (select id from hrmdepartment where tlevel='" + tlevel + "')");
                        JSONObject depdataJson = JSONObject.parseObject(depdata);
                        String errorDataStr = depdataJson.getString("errorData");
                        JSONArray errorDataArr = JSONArray.parseArray(errorDataStr);
                        for (int i = 0; i < errorDataArr.size(); i++) {
                            String key = errorDataArr.getJSONObject(i).getString("key");
                            String deptid = MaycurUtil.getDepartmentId(key);
                            rs.executeUpdate("update hrmdepartmentdefined set sfytb=1 where deptid='" + deptid + "' and isnull(sfytb,1) =0");
                        }
                    }
                }
            }

            //同步人员到每刻-人员在职
            String ryzt = "0,1,2,3";
            String rylt = "4,5,6,7";
            JSONArray employeeList = new JSONArray();
            rs.executeQuery("select a.id,a.workcode,a.lastname,a.email,a.mobile,a.departmentid,b.field4,a.companystartdate,a.sex,a.managerid from hrmresource a join cus_fielddata b " +
                    "on a.id=b.id where scope='HrmCustomFieldByInfoType' and scopeid='3' and isnull(b.field3,1) !=0 order by a.id");
            while (rs.next()) {
                JSONObject employeeJson = new JSONObject();
                String id = Util.null2String(rs.getString("id"));
                String workcode = Util.null2String(rs.getString("workcode"));
                String lastname = Util.null2String(rs.getString("lastname"));
                String email = Util.null2String(rs.getString("email"));
                String mobile = Util.null2String(rs.getString("mobile"));
                String departmentid = Util.null2String(rs.getString("departmentid"));
                String departmentCode = MaycurUtil.getDepartmentCode(departmentid);
                String joblevel = Util.null2String(rs.getString("field4"));
                String cbzx = MaycurUtil.getCbzx(id);
                String companystartdate = Util.null2String(rs.getString("companystartdate"));
                String sex = Util.null2String(rs.getString("sex"));
                String managerid = Util.null2String(rs.getString("managerid"));
                String resstatus = Util.null2String(rs.getString("status"));
                employeeJson.put("resourceid", id);
                employeeJson.put("employeeId", workcode);
                employeeJson.put("employeeName", lastname);
                employeeJson.put("email", email);
                employeeJson.put("phoneNo", mobile);
                JSONArray departments = new JSONArray();
                JSONObject departmentsJson = new JSONObject();
                departmentsJson.put("businessCode", departmentCode);
                departmentsJson.put("defaultDepartment", true);
                departmentsJson.put("supervisorEmployeeId", MaycurUtil.getWorkCode(managerid));
                departments.add(departmentsJson);
                employeeJson.put("departments", departments);
                JSONObject customObject = new JSONObject();
                customObject.put("CBZX", cbzx);
                employeeJson.put("customObject", customObject);
                if (ryzt.contains(resstatus)) {
                    resstatus = "ENABLE";
                }
                if (rylt.contains(resstatus)) {
                    resstatus = "DISABLE";
                }
                employeeJson.put("status", resstatus);
                employeeJson.put("hireDate", companystartdate);
                String subcompanyid = MaycurUtil.getsubcompanyid1(departmentid);
                employeeJson.put("defaultLegalEntityBizCode", "100" + subcompanyid);
                employeeJson.put("classPositionBizCode", joblevel);
                String gender = "0".equals(sex) ? "MAN" : "WOMAN";
                employeeJson.put("gender", gender);
                employeeJson.put("allowEmailAndPhoneBeNull", false);
                employeeList.add(employeeJson);
            }
            baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>employeeBatchAdd>>>>employeeList=" + employeeList + "<br/>");
            if (employeeList.size() > 0) {
                String responseStr = MaycurUtil.employeeBatch(employeeList, entCode, tokenId);
                baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>employeeBatchAdd>>>>responseStr=" + responseStr + "<br/>");
                JSONObject empresponseJson = JSONObject.parseObject(responseStr);
                String empstatus = empresponseJson.getString("success");
                String empdata = empresponseJson.getString("data");
                if ("true".equals(empstatus)) {
                    rs.executeUpdate("update cus_fielddata set field3=0 where scope='HrmCustomFieldByInfoType' and scopeid='3' and isnull(field3,1) !=0");
                }
                if ("false".equals(empstatus)) {
                    rs.executeUpdate("update cus_fielddata set field3=0 where scope='HrmCustomFieldByInfoType' and scopeid='3' and isnull(field3,1) !=0");
                    JSONObject empdataJson = JSONObject.parseObject(empdata);
                    String errorDataStr = empdataJson.getString("errorData");
                    JSONArray errorDataArr = JSONArray.parseArray(errorDataStr);
                    for (int i = 0; i < errorDataArr.size(); i++) {
                        String key = errorDataArr.getJSONObject(i).getString("key");
                        String resourceid = MaycurUtil.getResourceId(key);
                        rs.executeUpdate("update cus_fielddata set field3=1 where scope='HrmCustomFieldByInfoType' and scopeid='3' and isnull(field3,1) =0 and id='" + resourceid + "'");
                    }
                }
                //更新部门主管工号到每刻
                JSONArray departmentList1 = new JSONArray();
                for (int i = 0; i < departmentsList.size(); i++) {
                    JSONObject departmentJson = new JSONObject();
                    String departid = departmentsList.getJSONObject(i).getString("departid");
                    String businessCode = departmentsList.getJSONObject(i).getString("businessCode");
                    String name = departmentsList.getJSONObject(i).getString("name");
                    String parentBizCode = departmentsList.getJSONObject(i).getString("parentBizCode");
                    departmentJson.put("businessCode", businessCode);
                    departmentJson.put("newBusinessCode", businessCode);
                    departmentJson.put("name", name);
                    departmentJson.put("parentBizCode", parentBizCode);
                    String bmzgid = MaycurUtil.getDepartDefined(departid);
                    String managerEmployeeId = MaycurUtil.getWorkCode(bmzgid);
                    String[] managerEmployeeIds = {managerEmployeeId};
                    departmentJson.put("managerEmployeeIds", managerEmployeeIds);
                    departmentList1.add(departmentJson);
                }
                if (!departmentList1.isEmpty()) {
                    responseStr = MaycurUtil.departmentBatch(departmentList1, entCode, tokenId);
                    baseBean.writeLog(">>>>WeaSyncToMaycurCronJob>>>>>departmentBatchUpdate>>>>responseStr=" + responseStr);
                }
            }
        }
    }


}
